package com.wms.test.appcanServer.login;

import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.stereotype.Service;

import com.wms.interfc.InterfaceDoRun;


@Service("login")
public class ValidUserInfo implements InterfaceDoRun{
	
	private static final  String NAME ="user";
	private static final  String PWD ="pwd";
	
	public String doRun(String content)throws Exception{
		
		System.out.println("****************************************************");
		System.out.println(content);
		
		ObjectMapper obm = new ObjectMapper();
		
		UserInfoDomain domain = obm.readValue(content,UserInfoDomain.class);
		
		Map<String,String>  response = new HashMap<String, String>();
		
		if(!domain.getUsername().equals(NAME)){
			response.put("status", "failure");
			response.put("content","账号错误，默认账号 user 。");
			return (convertMap2String(response));
		}
		
		if(!domain.getUserpwd().equals(PWD)){
			response.put("status", "failure");
			response.put("content","密码错误，默认密码 pwd 。");
			return (convertMap2String(response));
		}
		
		response.put("status", "success");
		return (convertMap2String(response));
	}
	
	private String convertMap2String(Map<String,String> map)throws Exception{
		ObjectMapper om = new ObjectMapper();//jackson 转换器，将object 转换成json string
		StringWriter sw = new StringWriter();//jackson 转换时用到的输出流
		
		om.writeValue(sw, map);//进行转换
		
		sw.flush();
		sw.close();//关闭输出流
		
		System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
		System.out.println(sw.toString());
		
		return java.net.URLEncoder.encode(sw.toString(),"UTF-8");
	}
	
	
}
